#shaiya 3DO parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class Shaiya3DO(Model3D):
    
    def __init__(self, obj3D=None, inFile=None, outFile=None):
        '''Initialize am MQO Object.'''
        
        super(Shaiya3DO, self).__init__("Shaiya3DO")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_material(self, filename):
      
        self.create_material(0)
        self.add_material_name(0, filename)
        self.add_texture_name(0, filename + ".png")
        
    def parse_file(self, objName):
        
        self.create_object(objName)
        
        self.inFile.seek(0x4, os.SEEK_SET)
        
        vertNum = self.inFile.read_long()
        print vertNum
        
        for i in range(0, vertNum):
            vx, vy, vz = self.inFile.read_float(3)
            self.inFile.seek(12, os.SEEK_CUR)
            tu, tv = self.inFile.read_float(2)
            
            vx = float(vx) * -1
            
            self.create_vertex(objName, i)
            self.add_coords(objName, i, [vx, vy, vz])
            self.add_vert_uv(objName, i, [tu, tv])
            
        faceNum = self.inFile.read_long()
        for i in range(0, faceNum):
            v1, v2, v3 = self.inFile.read_short(3)
            
            self.create_face(objName, i)
            self.add_face_verts(objName, i, [v1, v2, v3])
            self.add_face_material(objName, i, 0)
                        
def read_file(path):
   
    openFile = StructReader(open(path, 'rb'))
    filename, ext = os.path.splitext(os.path.basename(path))
    obj = Shaiya3DO(inFile=openFile)
    obj.parse_material(filename)
    obj.parse_file(filename)
    openFile.close()
    return obj

def definitions():
    
    return "", "3DO", "Shaiya 3DO"

if __name__ == '__main__':
    filename = "drop_item002.3DO"
    obj = open(filename, 'rb')